﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 编码规则
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class BarCodeRuleController : BaseController
    {
        private readonly IBarCodeRuleService barCodeRuleService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="barCodeRuleService"></param>
        public BarCodeRuleController(IWebHostEnvironment environment, IBarCodeRuleService barCodeRuleService) : base(environment)
        {
            this.barCodeRuleService = barCodeRuleService;
        }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCodeRule(BarCodeRule dto)
        {
            bool rule = barCodeRuleService.AddCodeRule(dto);
            if (rule)
            {       
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败,该规则类型已存在！", "500"));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCodeRule(BarCodeRule dto)
        {
            if (dto.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dto.Code))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            bool rule = barCodeRuleService.UpdateCodeRule(dto);
            if (rule)
            {
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 删除编码规则
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelCodeRule(long ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool rule = barCodeRuleService.DelCodeRule(ids);
            if (rule)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 批量删除编码规则
        /// </summary>
        /// <param name="idsList"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult DelCodeRuleList(long[] idsList)
        {
            if (idsList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var ids in idsList)
            {
                bool rule = barCodeRuleService.DelCodeRule(ids);
              
                if (!rule)
                {
                    daRes = false;
                    break;
                }
            }

            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCodeRules(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var list = barCodeRuleService.GetCodeRules(name, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCodeRule(long ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = barCodeRuleService.GetCodeRule(ids);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 获取编码规则
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet,Obsolete]
        public IActionResult GetBarCodeSample(int type)
        {
            if (type <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            string? result = barCodeRuleService.GetBarCodeSample(type);
            if (!string.IsNullOrEmpty(result))
            {
                return Ok(BaseResult.Result(result, "获取成功！", "200"));
            }
            return Ok(BaseResult.Result(null, "无规则或规则已禁用！", "501"));
        }

        /// <summary>
        /// 枚举编码规则
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEnumCodeRulesType()
        {
            var list = BaseGetEnum.GetCodeRulesList<CodeRulesType>();

            return Ok(list);
        }
    }
}
